package com.qx.net;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.qx.iconst.CodeConst;
import com.qx.iconst.PathConst;

/**
 * 描述：<br>
 * HTTP工具类
 * 
 * @author LazyBear
 * @data 2014-4-17
 */
public class HttpHelper {

	private static Properties pro;

	private static InputStream in;
	
	/**
	 * 
	 * @author LazyBear 描述:<br>
	 *         发送请求
	 * @param order
	 *            命令号
	 * @param json
	 *            参数
	 * @return
	 */
	public static String request(int order, JsonObject json) {
		initAll();
		String url = null;
		switch (order) {
		case CodeConst.reg_order:
			url = pro.getProperty("reg");
			break;
		case CodeConst.createRole_order:
			url = pro.getProperty("createRole");
			break;
		case CodeConst.login_order:
			url = pro.getProperty("login");
			break;
		}
		if (url == null)
			return null;
		return sendGet(initArgs(url, json));
	}

	/**
	 * @author LazyBear 描述:<br>
	 *         发送get请求
	 * @param url
	 * @return
	 */
	private static String sendGet(String url) {
		CloseableHttpClient httpClient = HttpClients.createDefault();
		HttpGet get = new HttpGet(url);
		try {
			StringBuffer responseData = new StringBuffer();
			String line;
			CloseableHttpResponse response = httpClient.execute(get);
			BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
			line = reader.readLine();
			while (line != null) {
				responseData.append(line);
				line = reader.readLine();
			}
			return responseData.toString();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 
	 * @author LazyBear 描述:<br>
	 *         初始化input对象
	 */
	private static void initInput() {
		if (in == null)
			try {
				in = new BufferedInputStream(new FileInputStream(PathConst.httpCode_cfg));
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}

	/**
	 * 
	 * @author LazyBear 描述:<br>
	 *         初始化pro对象
	 */
	private static void initPro() {
		if (pro == null)
			pro = new Properties();
		try {
			pro.load(in);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * @author LazyBear 描述:<br>
	 *         初始化
	 */
	private static void initAll() {
		initInput();
		initPro();
	}

	/**
	 * 
	 * @author LazyBear 描述:<br>
	 *         以json格式初始化参数
	 * @param url
	 * @param json
	 * @return
	 */
	private static String initArgs(String url, JsonObject json) {
		StringBuffer sb = new StringBuffer(url).append("?");
		Set<Entry<String, JsonElement>> data = json.entrySet();
		Iterator<Entry<String, JsonElement>> data_ = data.iterator();
		while (data_.hasNext()) {
			Entry<String, JsonElement> entry = data_.next();
			sb.append(entry.getKey()).append("=").append(entry.getValue().getAsString());
			if (data_.hasNext())
				sb.append("&");
		}
		return sb.toString();
	}
}
